package com.cgnpc.scp.common.service;

import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.client.RestClient;
import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.config.Constants;
import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.result.ApiResult;
import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.vo.CgnRequestHeader;
import com.cgnpc.cud.boot.autoconfigure.aep.AepProperties;
import com.cgnpc.framework.utils.CgnHeader;
import com.cgnpc.scp.common.bean.ScpAepProperties;
import com.cgnpc.scp.common.mybatis.entity.ScpCommonSmsInfo;
import com.cgnpc.scp.common.utils.JacksonUtil;
import com.cgnpc.scp.common.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 短信服务
 */
@Service
public class SmsCenterService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SmsCenterService.class);

    @Autowired
    private RestClient restClient;

    @Autowired
    private AepProperties aepProperties;

    @Autowired
    private ScpAepProperties scpAepProperties;

    /**
     * 通过短信服务发送短信消息
     */
    public ApiResult sendSmsToSmsCenter(
            ScpCommonSmsInfo reqScpCommonSmsInfo
    ) throws Exception {
        //请求参数 json
        //{
        //"appId": 0,
        //"appName": "应用名称 ",
        //"content": "测试发送短信 ",
        //"staffNos": "P123456",
        //"requestStaffNo": " P133333"
        //}
        //
        //响应示例 json数据格式：
        //{
        //"responseId": null,
        //"code": "0",
        //"msg": "1条成功 0条失败 ",
        //"data": null
        //}

        final String logInfo = "[sendSmsToSmsCenter_UUID_" + UUID.randomUUID().toString() + "]";
        ApiResult result = null;

        try {
            if (reqScpCommonSmsInfo == null) {
                return null;
            }

            // begin 通过短信服务中心发送短信
            AepProperties usedAepProperties = aepProperties;
            ScpAepProperties usedScpAepProperties = scpAepProperties;
            LOGGER.info("{}usedAepProperties:{};", logInfo, JacksonUtil.writeValueAsString(usedAepProperties));

            Map<String, Object> reqBodyWithSms = new HashMap<>();

            // reqBodyWithSms.put("appId", StringUtil.formatStringToLen0(usedAepProperties.getAppId()));
//            reqBodyWithSms.put("appId", Long.valueOf(usedScpAepProperties.getAppIdParam()));
            reqBodyWithSms.put("appId", 0);
            reqBodyWithSms.put("appName", StringUtil.formatStringToLen0(usedAepProperties.getAppName()));
            reqBodyWithSms.put("content", StringUtil.formatStringToLen0(reqScpCommonSmsInfo.getContent()));
            reqBodyWithSms.put("staffNos", StringUtil.formatStringToLen0(reqScpCommonSmsInfo.getStaffNos()));
//            reqBodyWithSms.put("timestamp", DateUtil.formatDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),new Date()));
            reqBodyWithSms.put("requestStaffNo", StringUtil.formatStringToLen0(reqScpCommonSmsInfo.getRequestStaffNo()));

            LOGGER.info("{}reqBodyWithSms:{};", logInfo, JacksonUtil.writeValueAsString(reqBodyWithSms));

            // reqServiceMethod为固定的值
            // 按员工号发送短信，多个员工号以英文符号的逗号分隔开
            final String reqServiceMethod = Constants.SMS_URL + "/sendSmsByStaffNos";
            String url = CgnHeader.getUrl(usedAepProperties.getActive()) + reqServiceMethod;
            LOGGER.info("{}url:{};", logInfo, url);

            CgnRequestHeader header = CgnHeader.getHeader(reqServiceMethod, usedAepProperties);
//            header.setTimestamp(DateUtil.formatDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),new Date()));
            LOGGER.info("{}CgnRequestHeader:{};", logInfo, JacksonUtil.writeValueAsString(header));

            // 调用远程接口
            result = restClient.postCgnVoForRest(url, header, reqBodyWithSms);
            LOGGER.info("{}result:{};", logInfo, result);
            // end 通过短信服务中心发送短信
        } catch (Exception e) {
            LOGGER.error("{}error;", logInfo, e);
            throw e;
        }

        return result;
    }

}
